package com.wu.foodsafetyplayer.app.desbridgewebview;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.AttributeSet;

public class MyWebView extends DWebView {

    private onScrollChangeCallback callback;

    public MyWebView(Context context) {
        super(getFixedContext(context));
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(getFixedContext(context), attrs);
    }

    public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(getFixedContext(context), attrs, defStyleAttr);
    }

    /**
     * 适配Android 21 - 23
     * @param context 上下文对象
     * */
    public static Context getFixedContext(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return context.createConfigurationContext(new Configuration());
        } else {
            return context;
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (callback != null){
            callback.onScroll(l-oldl,t-oldt);
        }
    }

    public onScrollChangeCallback getOnScrollChangeCallback(){
        return callback;
    }

    public void setScrollChangeCallback(onScrollChangeCallback callback){
        this.callback = callback;
    }

    //设置回调借口，获取webview滑动的上下，左右距离差
    public static interface onScrollChangeCallback{
        public void onScroll(int dx, int dy);
    }
}
